// dtree_util.c
// ------------
// Written by Sanmay Das for CS 181, Spring 2000
// Updated by Dan Weiss for CS 181, Spring 2001
// Updated for clustering asst. SD, Spring 2001
// Do NOT modify this file

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <strstream>
#include "clust_util.h"

using namespace std;

// ValidateInput
// --------------
// Validate command line arguments
// Returns 0 on success; -1 on failure
int ValidateInput(int argc, char **argv, int *num_clusters, int *num_examples)
{
  
  if (argc != 3) {
    return -1;
  }
  *num_clusters = atoi(argv[1]);
  if (*num_clusters == 0) {
    return -1;
  }
  *num_examples = atoi(argv[2]);
  if (*num_examples == 0) {
    return -1;
  }
  
  return 0;
}

// ParseInput
// -----------
// Parses input from a file into an array of insts using C++ stream features
// Returns 0 on success; -1 on failure
int ParseInput(DatasetDescription DD, inst *dataset, int n, FILE *datafile)
{
  char buffer[MAXLINE];
  
  // For each line, read into buffer, then convert to a stream, then read from
  // the stream into dataset[i].attribute[j] or dataset[i].classifier
  for(int i = 0; i < n; i++) {
    fgets(buffer, MAXLINE, datafile);
    istrstream is(buffer, MAXLINE);
    for(int j = 0; j < DD.numattribs; j++) {
      is >> dataset[i].attribs[j];
      if(!is) {
        cout << "Error in datafile, line " << i << endl;
		return -1;
      }
    }
  }
  return 0;
}

// PrintOutput
// ------------
// Trivial function for printing the data stored in an inst array in the 
// format that it was read-in in
// Useful for error-checking
void PrintOutput(DatasetDescription DD, inst *dataset, int n)
{
  int i, j;

  for(i = 0; i < n; i++) {
    for(j = 0; j < DD.numattribs; j++) {
      cout << dataset[i].attribs[j] << ",";
    }
    cout << endl;
  }
}    
